layui.config({
    version: '1593348928630' //为了更新 js 缓存，可忽略
    });

layui.use(['layer', 'table', 'slider','form'], function(){
    var laydate = layui.laydate //日期
    , $ = layui.$
    , layer = layui.layer //弹层
    , table = layui.table //表格
    , form = layui.form
    , postUrls = jsRoutes.controllers.admin.SystemUsersController.osuser("kddc");

    //执行一个 table 实例
    var demotable = table.render({
    elem: '#demotable'
    ,contentType: 'application/json'
    ,headers: {"Csrf-Token": $('[name="csrfToken"]').val()}
    ,where:{sys:'kddc',op:'list'}
    ,method: postUrls.method
    , height: 550
    , url: postUrls.url //数据接口
    ,title: '用户表'
    ,page: true //开启分页
    ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
    ,totalRow: true //开启合计行
    ,cols: [[ //表头
    {type: 'checkbox', fixed: 'left'}
    ,{field: 'id', title: 'acct', width:180, sort: true, fixed: 'left'}
    ,{field: 'name', title: '数据中心', width:180}
     ,{field: 'test', title: '测试', width:80, sort: true}
    ]]
    });

    //监听头工具栏事件
    table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id)
    ,data = checkStatus.data; //获取选中的数据
    switch(obj.event){
    case 'add':
    layer.msg('添加');
    layer.open({
          type: 1
          , area: ['600px','450px']
          , shade: 0
          , content: $('.barDemoEdit')
          , btn: ['提交', '关闭']
          , yes: function (index, layero) {
            //按钮【按钮一】的回调
            console.log(index)
            var fdata = form.val("barDemoEditForm")
            console.log(fdata)
            var load_index = layer.load(1, { time: 30 * 1000 });
            $.post({
              url: postUrls.url,
              contentType: 'application/json; charset=UTF-8',
              headers: {"Csrf-Token": $('[name="csrfToken"]').val()},
              data: JSON.stringify({ op: "add", sys: "kddc",data:fdata}),
              //                 dataType: 'text',
              success: function (data) {
                console.log(data);
                layer.close(load_index);
                layer.msg("数据: \n" + data + "\n状态: " + status);
                demotable.reload();
              }
            });
            layer.close(index);
          }
          , btn2: function (index, layero) {
            //按钮【按钮二】的回调
            //return false 开启该代码可禁止点击该按钮关闭
          }
          , cancel: function () {
            //右上角关闭回调
            //return false 开启该代码可禁止点击该按钮关闭
          }

        });
    break;
    case 'update':
    if(data.length === 0){
    layer.msg('请选择一行');
    } else if(data.length > 1){
    layer.msg('只能同时编辑一个');
    } else {
    console.log(checkStatus.data[0])
    var testvar = ""
    if (checkStatus.data[0].test=='测试') testvar= 'on'
        form.val("barDemoEditForm",{
            "id":checkStatus.data[0].id
            ,"name":checkStatus.data[0].name
            ,"test":testvar
            ,"user":checkStatus.data[0].user
            //,"pwd":checkStatus.data[0].pwd
            ,"address":checkStatus.data[0].address
        })
        layer.open({
              type: 1
              , area: ['600px','450px']
              , shade: 0
              , content: $('.barDemoEdit')
              , btn: ['提交', '关闭']
              , yes: function (index, layero) {
                //按钮【按钮一】的回调
                console.log(index)
                var fdata = form.val("barDemoEditForm")
                console.log(fdata)
                var load_index = layer.load(1, { time: 30 * 1000 });
                $.post({
                  url: postUrls.url,
                  contentType: 'application/json; charset=UTF-8',
                  headers: {"Csrf-Token": $('[name="csrfToken"]').val()},
                  data: JSON.stringify({ op: "edit", sys: "kddc",data:fdata}),
                  //                 dataType: 'text',
                  success: function (data) {
                    console.log(data);
                    layer.close(load_index);
                    layer.msg("数据: \n" + data + "\n状态: " + status);
                    demotable.reload();
                  }
                });
                layer.close(index);
              }
              , btn2: function (index, layero) {
                //按钮【按钮二】的回调
                //return false 开启该代码可禁止点击该按钮关闭
              }
              , cancel: function () {
                //右上角关闭回调
                //return false 开启该代码可禁止点击该按钮关闭
              }

            });
    }
    break;
    case 'delete':
    if(data.length === 0){
    layer.msg('请选择一行');
    } else {
    layer.msg('删除');
    console.log(data)
    }
    break;
    };
    });

    });